home *** CD-ROM | disk | FTP | other *** search
- unit TimeOut;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, ExtCtrls;
-
- const
- DefaultTimeOut = 10000;
-
- type
- TTimeOut = class(TComponent)
- private
- FTimeOut: Cardinal;
- protected
- procedure SetTimeOut(Value: Cardinal);
- procedure TimerTick(Sender: TObject);
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- published
- property TimeOut: Cardinal
- read FTimeOut write SetTimeOut default DefaultTimeOut;
- end;
-
- procedure Register;
-
- implementation
-
- var
- hkKb, hkMouse: HHook;
- Timer: TTimer;
-
- function HookProc(HHook, Code: Integer; WParam: Cardinal; LParam: Longint): Longint;
- begin
- Timer.Enabled := False;
- Timer.Enabled := True;
- Result := CallNextHookEx(HHook, Code, WParam, LParam)
- end;
-
- function HookProcKB(Code: Integer; WParam: Cardinal; LParam: Longint): Longint;
- {$ifdef Win32}stdcall{$else}far{$endif};
- begin
- Result := HookProc(hkKB, Code, WParam, LParam)
- end;
-
- function HookProcMouse(Code: Integer; WParam: Cardinal; LParam: Longint): Longint;
- {$ifdef Win32}stdcall{$else}far{$endif};
- begin
- Result := HookProc(hkMouse, Code, WParam, LParam)
- end;
-
- const
- Count: Byte = 0;
-
- constructor TTimeOut.Create(AOwner: TComponent);
- begin
- if Count > 0 then
- raise Exception.Create('Only one of these time-out components allowed');
- Inc(Count);
- inherited Create(AOwner);
- FTimeOut := DefaultTimeout;
- if not (csDesigning in ComponentState) then
- begin
- Timer := TTimer.Create(Self);
- Timer.OnTimer := TimerTick;
- Timer.Interval := FTimeOut;
- {$ifdef Win32}
- hkKb := SetWindowsHookEx(wh_Keyboard, @HookProcKB, 0, GetCurrentThreadID);
- hkMouse := SetWindowsHookEx(wh_Mouse, @HookProcMouse, 0, GetCurrentThreadID);
- {$else}
- hkKb := SetWindowsHookEx(wh_Keyboard, HookProcKB, HInstance, GetCurrentTask);
- hkMouse := SetWindowsHookEx(wh_Mouse, HookProcMouse, HInstance, GetCurrentTask);
- {$endif}
- end;
- end;
-
- destructor TTimeOut.Destroy;
- begin
- if not (csDesigning in ComponentState) then
- begin
- UnhookWindowsHookEx(hkKb);
- UnhookWindowsHookEx(hkMouse)
- end;
- inherited Destroy
- end;
-
- procedure TTimeOut.SetTimeOut(Value: Cardinal);
- begin
- if FTimeOut <> Value then
- begin
- FTimeOut := Value;
- Timer.Interval := Value;
- end
- end;
-
- procedure TTimeOut.TimerTick(Sender: TObject);
- begin
- Application.Terminate
- end;
-
- procedure Register;
- begin
- RegisterComponents('Clinic', [TTimeOut]);
- end;
-
- end.